<?php
class Siswa_pindah_kelas extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('siswa_pindah_kelas_model');
		$this->load->model('master_model');
		$this->load->model('pelajaran_model');
	}
	
	function index()
	{
		$data['value_tingkat'] = '';
		$data['value_kelas'] = '';
		
			if($this->input->post('tingkat')!=''){
				$this->session->set_userdata('sess_pg_pindah_tingkat', $this->input->post('tingkat'));
			}
			
			if($this->input->post('kelas')!='' ){
				$this->session->set_userdata('sess_pg_pindah_kelas', $this->input->post('kelas'));
			}
			
			$data['value_tingkat'] = $this->session->userdata('sess_pg_pindah_tingkat');
			$data['value_kelas'] = $this->session->userdata('sess_pg_pindah_kelas');
			
		//ver lib
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		$colModel['NISN'] = array('NISN',100,TRUE,'center',1);
		$colModel['NAMA_LENGKAP'] = array('Nama lengkap',200,TRUE,'center',1);
		$colModel['TEMPAT_LAHIR'] = array('Tempat Lahir',80,TRUE,'center',1);
		$colModel['TANGGAL_LAHIR'] = array('Tanggal Lahir',80,TRUE,'center',1);
		$colModel['ALAMAT_DOMISILI'] = array('Alamat',300,TRUE,'center',1);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 10,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Siswa  >>  Pindah Kelas'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		
		$buttons[] = array('separator');
		$buttons[] = array('Select All','add','org_js');
		$buttons[] = array('Deselect All','delete','org_js');
		$buttons[] = array('separator');
		
		if($data['value_tingkat']!='' and $data['value_kelas']!='' )
			$buttons[] = array('Pindahkan','next','org_js');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		
		if($data['value_tingkat']!='' and $data['value_kelas']!='' )
			$grid_js = build_grid_js('flex1',site_url("/siswa_pindah_kelas/listsiswa_grid/".$data['value_tingkat']."/".$data['value_kelas']),$colModel,'no','asc',$gridParams,$buttons);
		else
			$grid_js = build_grid_js('flex1',site_url("/siswa_pindah_kelas/listsiswa_grid/"),$colModel,'no','asc',$gridParams,$buttons);
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] = '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Pindahkan')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin memindah kelas ' + $('.trSelected',grid).length + ' Siswa?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/siswa_pindah_kelas/pindah_siswa')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";	
			
		$data['tingkat'] = $this->master_model->combo_tingkat_kelas($this->session->userdata('unitid'));
		$data['kelas'] = $this->master_model->combo_kelas($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$data['value_tingkat']);
		
		if($data['value_tingkat']!='' and $data['value_kelas']!='' )
			$data['kelas_tujuan'] = $this->master_model->combo_kelas($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$data['value_tingkat']);
		
		$data['width'] = '750';
		$data['url_filter'] = 'siswa_pindah_kelas/index';
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listsiswa_grid($id_tingkat='',$id_kelas='')
	{
		
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('NISN','NAMA_LENGKAP','TEMPAT_LAHIR','TANGGAL_LAHIR','ALAMAT_DOMISILI');
		
		$this->flexigrid->validate_post('a.NISN','asc',$valid_fields);

		$records = $this->siswa_pindah_kelas_model->get_siswa_kelas($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$id_tingkat,$id_kelas);
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		if($records['record_count'] != 0 ){
		 
			foreach ($records['records']->result() as $row)
			{
				$record_items[] = array($row->NISN,
				$no+1,
				'<div align="center">'.$row->NISN.'</div>',
				'<div align="center">'.$row->NAMA_LENGKAP.'</div>',
				'<div align="left">'.$row->TEMPAT_LAHIR.'</div>',
				'<div align="left">'.$row->TANGGAL_LAHIR.'</div>',
				'<div align="left">'.$row->ALAMAT_DOMISILI.'</div>'
				);
				$no++;
			}
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	//Delete
	function pindah_siswa()
	{
		if($this->session->userdata('sess_kelas_tujuan') != 0 || $this->session->userdata('sess_kelas_tujuan') != '' ){
		if($this->session->userdata('sess_kelas_tujuan') != $this->session->userdata('sess_pg_pindah_kelas')){
		$ta_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($ta_ids_post_array as $index => $ta_id){
			if (isset($ta_id) && $ta_id != "") {
				
				$data_pindah_kelas = array(
					'ID_SEKOLAH'			=> $this->session->userdata('unitid'),
					'ID_TAHUNAJARAN'		=> $this->session->userdata('tahunajaran'),
					'ID_TINGKAT'			=> $this->session->userdata('sess_pg_pindah_tingkat'),
					'ID_KELAS'				=> $this->session->userdata('sess_pg_pindah_kelas'),
					'NISN'					=> $ta_id,
					'ID_KELAS_TUJUAN'		=> $this->session->userdata('sess_kelas_tujuan')
				);
				
				$data_update_kelas = array(
					'ID_KELAS'		=> $this->session->userdata('sess_kelas_tujuan')
				);
				
				// print_r($this->session->userdata('sess_kelas_tujuan'));exit;
				$this->siswa_pindah_kelas_model->update_kelas_siswa($data_update_kelas,$ta_id);
				$this->siswa_pindah_kelas_model->insert_pindah_siswa($data_pindah_kelas);
				
			}
		}
		$msg .= "Pemindahan Kelas Siswa berhasil.\n";
		
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
		
		}
		else $this->output->set_output('Kelas Tujuan tidak boleh sama dengan kelas asal');
		} 
		else $this->output->set_output('Kelas Tujuan Harus dipilih terlebih dahulu');
	}
    
}
?>
